import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.Callable;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.Exchanger;

public class SiSuoDemo {
    private static Object resource1 = new Object();
    private static Object resource2 = new Object();
    public static void main(String[] args) {
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                synchronized (resource1){
//                    System.out.println(" 线程1资源被占用 " );
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    synchronized (resource2){
//                        System.out.println(" 线程1要占用线程2资源 " );
//                    }
//                }
//            }
//        }).start();
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                synchronized (resource2){
//                    System.out.println("线程2资源被占用");
//                    try {
//                        Thread.sleep(1000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    synchronized (resource1){
//                        System.out.println(" 线程2请求到线程1的资源 " );
//                    }
//                }
//            }
//        }).start();
//        CyclicBarrier cb = new CyclicBarrier(5,new Meeting());
//        for (int i = 1; i <= 10; i++) {
//            new EmployeeThread("员工"+i,cb).start();
//        }
        Exchanger exchanger = new Exchanger();
        new Boy(exchanger).start();
        new girl(exchanger).start();
    }
}
class Boy extends Thread{
    private Exchanger exchanger;
    public Boy(Exchanger exchanger) {
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        System.out.println("男孩开始活动");
        try {
            Object o = exchanger.exchange("🐕物品");
            System.out.println("男孩收到"+o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class girl extends Thread{
    private Exchanger exchanger;
    public girl(Exchanger exchanger) {
        this.exchanger = exchanger;
    }

    @Override
    public void run() {
        System.out.println(" 女孩开始活动 " );
        try {
            Object o = exchanger.exchange("❤物品");
            System.out.println("女孩收到"+o);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
class Meeting implements Runnable{

    @Override
    public void run() {
        System.out.println("Thread.currentThread().getName() = " + Thread.currentThread().getName()+"组织会议");
    }
}
class EmployeeThread extends Thread{
    private CyclicBarrier cb;

    public EmployeeThread( String name, CyclicBarrier cb) {
        super(name);
        this.cb = cb;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName()+"进入会议室");
        try {
            cb.await();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (BrokenBarrierException e) {
            e.printStackTrace();
        }
    }
}
